﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using SIMS.Shared.DTO;
using SIMS.Shared.Models;
using SIMS.SqlSugarWebApi.Models;
using SIMS.SqlSugarWebApi.ServiceInterface;

namespace SIMS.SqlSugarWebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IRoleService _service;

        private readonly IMapper _mapper;

        public RoleController(IRoleService service, IMapper mapper)
        {
            _service = service;
            _mapper = mapper;
        }

        [HttpPost]
        public async Task<IActionResult> AddRole(RoleDTO r)
        {
            var role = _mapper.Map<Role>(r);
            var data = await _service.AddEntity(role);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpDelete]
        public async Task<IActionResult> DeleteRole(int id)
        {
            var data = await _service.DeleteEntity(id);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpGet]
        public async Task<IActionResult> GetRole(int id)
        {
            var data = await _service.GetEntity(id);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpPut]
        public async Task<IActionResult> UpdateRole(RoleDTO r)
        {
            var role = _mapper.Map<Role>(r);
            var data = await _service.UpdateEntity(role);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpGet]
        public async Task<IActionResult> GetRoles(string? roleName, int pageNum, int pageSize)
        {
            var data = await _service.GetRoles(roleName, pageNum, pageSize);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpGet]
        public async Task<IActionResult> GetUserRights(int? userId)
        {
            var data = await _service.GetUserRights(userId);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpGet]
        public IActionResult GetRoleMenus(int? roleId)
        {
            var data = _service.GetRoleMenus(roleId);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpGet]
        public async Task<IActionResult> SetRoleMenus(int roleId, string menuIds)
        {
            var data = await _service.SetRoleMenus(roleId, menuIds);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }
    }
}
